package com.mj.factory.processFlow;

import com.mj.factory.productOrder.ProductOrder;
import com.mj.factory.productOrder.ProductOrderService;
import com.mj.factory.totalSchuedule.TotalScheduleService;
import com.mj.factory.workOrder.WorkOrder;
import com.mj.factory.workOrder.WorkOrderService;
import com.mj.factory.workOrder.WorkOrderStatus;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/16.
 */
@Controller
@RequestMapping("mjfactory")
/*
1.增加(增加项目同时可以对工艺进行增删)
2.修改基本信息(修改项目信息)
3.修改项目工艺(修改项目的工艺信息可对工艺进行增删改)
4.删除(删除项目同时删除项目下的工艺)
5.查看(查看项目下的工艺信息)
*/
public class ProcessFlowController {
    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    WorkOrderService workOrderService;
    @Autowired
    ProcessFlowService processFlowService;
    @Autowired
    TotalScheduleService totalScheduleService;
    @Autowired
    ProductOrderService productOrderService;

    //增加(增加项目同时可以对工艺进行增删)
    @RequestMapping(value = "processFlow/addProcessFlow", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addProcessFlow(long workOrderId, long projectId,
                                              String showPartsName, String showChartNumber, String showOderNumber,
                                              Integer sequenceNumber, String productionProcesses,
                                              String processContent, Integer productionQuantity,
                                              float estimateTime, String operator) {
        Map<String, Object> map = new HashedMap();

        ProductOrder productOrder = productOrderService.findById(projectId);
        if (productOrder == null) {
            map.put("msg", "error");
            map.put("result", "该项目不存在。");
            return map;
        }

        ProcessFlow processFlow = new ProcessFlow();
        processFlow.setProductionProcesses(productionProcesses);
        processFlow.setSequenceNumber(sequenceNumber);
        processFlow.setProcessContent(processContent);
        processFlow.setProductionQuantity(productionQuantity);
        processFlow.setEstimateTime(estimateTime);
        processFlow.setOperator(operator);
        try {
            WorkOrder updateWO;
            WorkOrder workOrder;
            if (workOrderId != 0) {
                //执行修改工单操作
                updateWO = workOrderService.findById(workOrderId);
                List<ProcessFlow> processFlowList = processFlowService.findByWorkOrderIdAndSequenceNumber(workOrderId, sequenceNumber);
                if (processFlowList.size() != 0) {
                    map.put("msg", "error");
                    map.put("result", "工单序号不能重复。");
                    return map;
                }
                if (processFlow != null) {
                    updateWO.setWorkOrderStatus(WorkOrderStatus.StatusEnum.Ready.toString());
                }
                workOrder = workOrderService.save(updateWO);
                processFlow.setWorkOrder(updateWO);
                processFlowService.save(processFlow);
                //修改总表数据
                totalScheduleService.saveWithTechnologicalProcess(updateWO);
            } else {
                //执行添加工单操作
                workOrder = workOrderService.findByChartNumber(showChartNumber);
                if (workOrder == null) {
                    workOrder = new WorkOrder();
                }
                workOrder.setChartNumber(showChartNumber);
                workOrder.setPartName(showPartsName);
                workOrder.setOderNumber(showOderNumber);
                workOrder.setProductOrder(productOrder);
                workOrder.setWorkOrderStatus(WorkOrderStatus.StatusEnum.Ready.toString());
                workOrder = workOrderService.save(workOrder);
                processFlow.setWorkOrder(workOrder);
                processFlowService.save(processFlow);
                totalScheduleService.saveWithTechnologicalProcess(workOrder);
            }
            map.put("workOrder", workOrder);
            map.put("msg", "success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    //删除工序
    @RequestMapping(value = "processFlow/deleteProcessFlow", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> deleteProcessFlow(String ids) {
        Map<String, String> map = new HashedMap();
        String[] idArrayList = ids.split(",");
        List<ProcessFlow> list = new ArrayList<ProcessFlow>();
        for (int i = 0; i < idArrayList.length; i++) {
            ProcessFlow processFlow = new ProcessFlow();
            processFlow.setId(Long.valueOf(idArrayList[i]));
            list.add(processFlow);
        }
        boolean flag = processFlowService.delete(list);
        if (flag) {
            map.put("msg", "success");
        } else {
            map.put("msg", "fail");
        }
        return map;
    }

    /**
     * @param workOrderId
     * @author cRyann
     * @Description 获取工单的详细工艺流程
     **/
    @PostMapping(value = "processFlow/findProcessFlowByWorkOrderId")
    @ResponseBody
//    @CheckPermission(permission={"mj_totalschedule:view"})
    public Map<String, Object> findAll(long workOrderId) {
        Map<String, Object> map = new HashedMap();
        List<ProcessFlow> list = processFlowService.findByWorkOrderIdOrderBySequenceNumber(workOrderId);
        int total = list.size();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    /**
     * @param
     * @author cRyann
     * @Description 工艺师使用、修改工艺流程
     **/
    @RequestMapping(value = "processFlow/updateProcessFlow", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateProcessFlow(String tpProductionProcesses,
                                                 long workOrderId, Integer tpSequenceNumber,
                                                 String tpProcessContent,
                                                 Integer tpProductionQuantity,
                                                 float tpEstimateTime, String tpOperator, long tpId) {
        ProcessFlow pf = new ProcessFlow();
        pf.setSequenceNumber(tpSequenceNumber);
        pf.setEstimateTime(tpEstimateTime);
        pf.setProductionQuantity(tpProductionQuantity);
        pf.setOperator(tpOperator);
        pf.setProcessContent(tpProcessContent);
        pf.setProductionProcesses(tpProductionProcesses);
        pf.setId(tpId);
        Map<String, Object> map = new HashedMap();
        List<ProcessFlow> processFlowList = processFlowService.findByWorkOrderIdAndSequenceNumber(workOrderId, tpSequenceNumber);
        if (processFlowList.size() != 0) {
            map.put("msg", "error");
            map.put("result", "工单序号不能重复。");
            return map;
        }
        WorkOrder wo = workOrderService.findById(workOrderId);
        pf.setWorkOrder(wo);

        try {
            processFlowService.save(pf);
            totalScheduleService.saveWithTechnologicalProcess(wo);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "fail");
            logger.error(e);
        }
        return map;
    }

    @RequestMapping(value = "processFlow/toUpdateProcessFlow", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> toUpdateProcessFlow(Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            ProcessFlow processFlow = processFlowService.findById(id);
            map.put("msg", "success");
            map.put("processFlow", processFlow);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "error");
        }

        return map;
    }

    /**
     * @param workOrderId
     * @author cRyann
     * @Description 生产管理页面下获取工序步骤
     **/
    @PostMapping(value = "processController/findProcessFlowByWorkOrderId")
    @ResponseBody
    public Map<String, Object> findForController(long workOrderId) {
        return findAll(workOrderId);
    }
}

